/**
 * Created by zycool on 2016/8/17.
 * 设备基础信息 - 设备管理
 */

;(function($,window,document,undefined){

    /*** 条件 ***/
    // 设备编号
    var $number = $('#JQ_number');
    // 型号
    var $type = $('#JQ_type');
    // 唯一识别码
    var $code = $('#JQ_code');
    // 购买时间1
    var $begindate = $('#JQ_begindate');
    // 购买时间2
    var $enddate = $('#JQ_enddate');
    // 设备状态
    var $state = $('#JQ_state').select3({search:false});

    /*** 新建 & 编辑 ***/
    var $number_edit = $('#JQ_number_edit');
    var $type_edit = $('#JQ_type_edit');
    var $code_edit = $('#JQ_code_edit');
    var $remark_edit = $('#JQ_remark_edit');
    var $date_edit = $('#JQ_buydate_edit');
    $date_edit.datetimepicker({
        timepicker: false,
        format: 'Y-m-d',
        scrollInput: false,
        style: 'z-index:102'
    });
    var $state_edit = $('#JQ_state_edit').select3({search:false});
    // 表格
    var $table_01 = $('#JQ_datatable_01');
    // 表单
    var $action_form = $('#JQ_action_form');
    // 填充设备状态 - 条件 - 新建
    DyyBase.ajax(deviceApi.url_base_state(),{},function(dataset){
        var json = DyyBase.getSelect(dataset);
        json.shift();
        $state.add(json);
        $state_edit.add(json);
    });

    // 条件对象
    var filter = {
        // 查询
        getCondition: function(data){
            var filter = {};
            filter.mode = true;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 设备编号
            filter.customNumber = function(){
                var v  = $number.val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 型号
            filter.modelNumber = function(){
                var v  = $type.val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 唯一识别码
            filter.uniqueCide = function(){
                var v  = $code.val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间1
            filter.beginbuyDate = function(){
                var v  = $begindate.val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.endbuyDate = function(){
                var v1  = $begindate.val();
                var v2  = $enddate.val();
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $enddate.TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            // 设备状态
            filter.state = function(){
                var v  = $state.val();
                if(v){
                    return v;
                }
                return '';
            }();

            return filter;
        },
        //排流装置导出参数
        getExportCondition:function(){
            return filter.getCondition();
        },
        // 新建
        getCondition2: function(){
            var filter = {};
            filter.mode = true;
            // ID
            filter.id = function(){
                var v  = $('#JQ_eqpt_id').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 设备编号
            filter.customNumber = function(){
                var v  = $number_edit.val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 型号
            filter.modelNumber = function(){
                var v  = $type_edit.val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 唯一识别码
            filter.uniqueCide = function(){
                var v  = $code_edit.val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间1
            filter.buyDate = function(){
                var v  = $date_edit.val();
                if(v && v != '-'){
                    return v;
                }
                return '';
            }();
            // 设备状态
            filter.state = function(){
                var v  = $state_edit.val();
                if(v){
                    return v;
                }else{
                    $('#JQ_state_edit').TIP({
                        content: '设备状态不能为空!'
                    });
                    filter.mode = false;
                }
                return '';
            }();
            // 设备状态
            filter.createRemark = function(){
                var v  = $remark_edit.val();
                if(v){
                    return v;
                }
                return '';
            }();

            return filter;
        }
    };

    // 初始加载表格
    var DT01 = $table_01.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': function () {
                return '<div class="zy-datatable-empty"><p><img src="' + DyyBase.root_path + '/static/img/no_data.png"></p><h2>报告领导 没有数据~</h2><p class="c-90">去换别的查询条件吧！</p></div>';
            },
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getCondition(data);
            DyyBase.ajax(deviceApi.url_base_list(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {},
        'columns': [
            {
                data: 'customNumber', render: function (data) {
                return '<span title="'+data+'">'+data+'</span>';
            }
            },
            {
                data: 'modelNumber', render: function (data) {
                return '<span title="'+data+'">'+data+'</span>';
            }
            },
            {
                data: 'uniqueCide', render: function (data) {
                return '<span title="'+data+'">'+data+'</span>';
            }
            },
            {
                data: 'buyDate', render: function (data) {
                return '<span title="'+data+'">'+data+'</span>';
            }
            },
            {
                data: 'stateStr', render: function (data) {
                return '<span title="'+data+'">'+data+'</span>';
            }
            },
            {
                data: 'id', render: function (data) {
                return '<i title="编辑" class="zy-record-add-icon" data-id="'+data+'"></i>&nbsp;&nbsp;&nbsp;<i title="删除" class="zy-list-icon-delete" data-id="'+data+'"></i>';
            }
            }
        ]
    });

    // 搜索
    $('#JQ_search_btn').click(function(){
        var param = filter.getCondition();
        if(param.mode){
            DT01.draw();
        }
    });

    // 删除
    $table_01.delegate('i.zy-list-icon-delete','click',function(){
        var $this = $(this);
        $this.MSG({
            type: 'confirm',
            content: '您确认要删除此设备吗？',
            yesBtnTxt: '确认',
            height: 230,
            yesBtnAct: function(){
                var id = $this.attr('data-id');
                DyyBase.ajax(deviceApi.url_base_delete(),{id:id},function(dataset){
                    if(dataset.bool){
                        $this.MSG({
                            type: 'success',
                            content: '删除成功!',
                            yesBtnAct: function(){
                                if(DT01){
                                    DT01.draw();
                                }
                            },
                            closeBtnAct: function(){
                                if(DT01){
                                    DT01.draw();
                                }
                            }
                        });
                    }
                });
            }
        });
    });

    // 导出
    $('#JQ_export_btn').click(function(){
        var param = filter.getExportCondition;
        if(param instanceof Function){
            param = param();
        }
        if(param.mode){
            $(this).EXPT({
                type: 'name',
                name: '设备管理信息',
                param: param,
                server: DyyBase.root_path + '/eqpt/equipment/equipmentInfo/exportQuipment.json'
            });
        }
    });

    // 编辑
    $table_01.delegate('i.zy-record-add-icon','click',function(){
        popupType('edit');
        var $this = $(this);
        var $tr = $this.parent('td').parent('tr');
        var data = DT01.row($tr).data();
        if(data){
            // 填充数据
            $('#JQ_eqpt_id').val(data.id);
            $('#JQ_number_edit').val(data.customNumber);
            $('#JQ_type_edit').val(data.modelNumber);
            $('#JQ_code_edit').val(data.uniqueCide);
            $('#JQ_buydate_edit').val(data.buyDate);
            $state_edit.fit(data.state);
            $('#JQ_remark_edit').val(data.createRemark);

            var wind = $this.WIND({
                width: 750,
                height: 430,
                title: '编辑',
                actionBox: 'div.zy-edit-eqpt',
                yesBtnAct: function(){
                    actionEqpt('edit',wind.clear);
                },
                closeBtnAct: function(){
                    $action_form.reset();
                    wind.clear();
                },
                noBtnAct: function(){
                    $action_form.reset();
                    wind.clear();
                }
            });
        }
    });

    // 新建
    $('#JQ_create_btn').click(function(){
        popupType('new');
        $(this).WIND({
            width: 750,
            height: 430,
            title: '新建',
            actionBox: 'div.zy-edit-eqpt',
            yesBtnAct: function(){
                actionEqpt('new');
            }
        });
    });

    function popupType(type){
        if(type == 'new'){
            $('#JQ_action_yes_btn').hide();
            $('#JQ_xyf_action_yes').show();
            $('#JQ_popup_type').val('new');
        }else if(type == 'edit'){
            $('#JQ_action_yes_btn').hide();
            $('#JQ_xyf_action_yes').show();
            $('#JQ_popup_type').val('edit');
        }else{
            $('#JQ_action_yes_btn').show();
            $('#JQ_xyf_action_yes').hide();
            $('#JQ_popup_type').val('');
        }
    }

    $('#JQ_xyf_action_yes').click(function(){
        var url, verify = $action_form.verify(),typeVal = $('#JQ_popup_type').val();
        if(verify === true){
            if(typeVal == 'edit'){
                url = deviceApi.url_base_edit();
            }else if(typeVal == 'new'){
                url = deviceApi.url_base_create();
            }

            var param = filter.getCondition2();
            if(param.mode){
                DyyBase.ajax(url,param,function(dataset){
                    if(dataset.bool){
                        $('#JQ_action_yes_btn').trigger('click');
                    }else{
                        $('#JQ_code_edit').TIP({content:dataset.result});
                    }
                });
                //$action_form.reset();
            }
        }
    });

    // 新建 / 编辑
    function actionEqpt(type,callback){
        if(callback instanceof Function){
            callback();
        }
        var title;
        if(type == 'edit'){
            title = '编辑';
        }else if(type == 'new'){
            title = '新建';
        }
        $table_01.MSG({
            type: 'success',
            content: title+'成功!',
            yesBtnAct: function(){
                if(DT01){
                    DT01.draw();
                }
            },
            closeBtnAct: function(){
                if(DT01){
                    DT01.draw();
                }
            }
        });
        $action_form.reset();

        /*var url, verify = $action_form.verify(), title = '';
        if(verify === true){
            if(data){
                url = deviceApi.url_base_edit();
                title = '编辑';
            }else{
                url = deviceApi.url_base_create();
                title = '新建';
            }

            var param = filter.getCondition2();
            if(param.mode){
                if(callback instanceof Function){
                    callback();
                }
                DyyBase.ajax(url,param,function(dataset){
                    if(dataset.bool){
                        $(this).MSG({
                            type: 'success',
                            content: title+'成功!',
                            yesBtnAct: function(){
                                if(DT01){
                                    DT01.draw();
                                }
                            },
                            closeBtnAct: function(){
                                if(DT01){
                                    DT01.draw();
                                }
                            }
                        });
                    }else{
                        $(this).MSG({
                            type: 'error',
                            content: dataset.result,
                            yesBtnAct: function(){
                                if(DT01){
                                    DT01.draw();
                                }
                            },
                            closeBtnAct: function(){
                                if(DT01){
                                    DT01.draw();
                                }
                            }
                        });
                    }
                });
                $action_form.reset();
            }
        }*/
    }

})(jQuery,window,document);